﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class HelpDoc: IHelpDoc
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public HelpDoc(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.HelpDoc GetHelpDoc(System.Int32 docID)
		{
			BEGDC.PreContract.DAL.HelpDoc helpDoc = BEGDC.PreContract.DAL.HelpDoc.TryFind(docID);

            return helpDoc;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetHelpDoc_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM HelpDoc";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteHelpDoc(System.Int32 docID)
		{
			try
            {
				BEGDC.PreContract.DAL.HelpDoc helpDoc = BEGDC.PreContract.DAL.HelpDoc.TryFind(docID);
                helpDoc.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("HelpDoc", string.Format("Error occured when deleting the HelpDoc, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddHelpDoc(System.String docDescription, System.String docPath, Nullables.NullableDateTime dateCreated, Nullables.NullableInt32 uploadedBy)	
		{
			try
            {
				BEGDC.PreContract.DAL.HelpDoc helpDoc = new BEGDC.PreContract.DAL.HelpDoc();
				
				helpDoc.DocDescription = docDescription;
				helpDoc.DocPath = docPath;
				helpDoc.DateCreated = dateCreated;
				helpDoc.UploadedBy = uploadedBy;
				helpDoc.CreateAndFlush();
				
				return helpDoc.DocID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("HelpDoc", string.Format("Error occured when adding a new HelpDoc, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateHelpDoc(System.Int32 docID, System.String docDescription, System.String docPath, Nullables.NullableDateTime dateCreated, Nullables.NullableInt32 uploadedBy)
		{
			try
            {
                BEGDC.PreContract.DAL.HelpDoc helpDoc = GetHelpDoc(docID);

				helpDoc.DocDescription = docDescription;
				helpDoc.DocPath = docPath;
				helpDoc.DateCreated = dateCreated;
				helpDoc.UploadedBy = uploadedBy;
				helpDoc.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("HelpDoc", string.Format("Error occured when updating the HelpDoc, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



